<?php
/**
 * Basic Logger that saves to an array in memory
 *
 * @author Alton Crossley <crossleyframework@nogahidebootstrap.com>
 * @package Crossley Framework
 *
 * @license BSD http://opensource.org/licenses/bsd-license.php
 * @version $Id:$
 *
 */

/**
 * @namespace
 */
namespace X\Logger;
use \X\Logger;

/**
 * simple memory logger
 *
 */
class Memory implements Logger
{
    /**
     * instance of Memory Logger
     * @var \X\Memory
     */
    protected static $_Instance = array();
    /**
     * memory storage of messages
     * @var unknown_type
     */
    protected $_messages = array();
    /* (non-PHPdoc)
	 * @see X.Logger::logMessage()
	 */
    public function logMessage($message, $MessageType = null)
    {
        if (!array_key_exists($MessageType, $this->_messages) ||
            !is_array($this->_messages[$MessageType]))
            {
                $this->_messages[$MessageType] = array();
            }
        $this->_messages[$MessageType][] = $message;
    }
    /* (non-PHPdoc)
	 * @see X.Logger::onCompletion()
	 */
    public function onCompletion(\Closure $Action, $SuccessMessage)
    {
        try
        {
            $Action();
        }
        catch (\Exception $e)
        {
            throw $e;
            return false;
        }
        $this->logMessage($SuccessMessage, 'success');
        return true;
    }
    
    /* (non-PHPdoc)
	 * @see X.Logger::tryCall()
	 */
    public function tryCall(\Closure $Action, $SuccessMessage, $FailureMessage)
    {
        try
        {
            $Action();
        }
        catch (\Exception $e)
        {
            $this->logMessage(sprintf($FailureMessage, $e), 'failure');
            return false;
        }
        $this->logMessage($SuccessMessage, 'success');
        return true;
    }
    
    /* (non-PHPdoc)
	 * @see X.Logger::tryFailure()
	 */
    public function tryFailure(\Closure $Action, $FailureMessage)
    {
        try
        {
            $Action();
        }
        catch (\Exception $e)
        {
            $this->logMessage(sprintf($FailureMessage, $e), 'failure');
            return false;
        }
        
        return true;
    }
    /**
     * retrieve the single instance of this class
     * @return \X\Logger\Memory
     */
    public static function getInstance($name = 0)
    {
        $thisClassName = __CLASS__;
        
        if (!array_key_exists($name, self::$_Instance) ||
            !isset(self::$_Instance[$name]))
                self::$_Instance[$name] = new $thisClassName();
                
        return self::$_Instance[$name];
    }

    /**
     * static wrapper for logMessage()
     * @see X.Logger::logMessage()
     */
    public static function slogMessage ($message, $MessageType = null)
    {
        self::getInstance()->logMessage($message, $MessageType);
    }
    /**
     * static wrapper for onCompletion()
     * @see X.Logger::onCompletion()
     */
    public static function sonCompletion (\Closure $Action, $SuccessMessage)
    {
        self::getInstance()->onCompletion($Action, $SuccessMessage);
    }
    /**
     * static wrapper for tryCall()
     * @see X.Logger::tryCall()
     */
    public static function stryCall (\Closure $Action, $SuccessMessage, $FailureMessage)
    {
        self::getInstance()->tryCall($Action, $SuccessMessage, $FailureMessage);
    }
    /**
     * static wrapper for tryFailure()
     * @see X.Logger::tryFailure()
     */
    public static function stryFailure (\Closure $Action, $FailureMessage)
    {
        self::getInstance()->tryFailure($Action, $FailureMessage);
    }
    
    public function __toString()
    {
        $OutputString = "";
        foreach ($this->_messages as $Key => $Value)
        {
            $OutputString .= sprintf('%1$s : %2$s\n', $Key, $Value);
        }
        return $OutputString;
    }
}